﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
namespace WPFExcelReport
{
    public partial class TAMSTAMM
    {

        public TAMSTAMM()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VAMSTAMM();
            this.DataContext = MV;
            InitializeComponent();
        }
        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            if (dwMaster == null)
            {
                /*
                AMTYP = "";
                AMART = "";
                AMKST = "";
                AMCD = "";*/
            }
            else
            {/*
                AMTYP = TMV.AMTYP;
                AMART = TMV.AMART;
                AMKST = TMV.AMKSTKRS + AMKST;
                AMCD = TMV.AMVERFKZ;
                */
                if (TMV.AMSTDVAR == "J")
                    IsSTD = true;
                else
                    IsSTD = false;
            }

        }
        public override void OnKRS()
        {
            if (KRS.Trim() == "M")
            {
                IsMM.IsChecked = true;
            }
            else
            {
                IsMM.IsChecked = false;
            }
            base.OnKRS();
        }

        public override void QueryCommand(object sender, RoutedEventArgs e)
        {

            TBMain.Clear();
            dwData = TMV.TBMain.NewRow();
            dwData["AMMANDANT"] = app.Mandant;
            dwData["AMNRKRS"] = " AND AMNRKRS LIKE '" + KRS.Trim() + "%'";
            dwData["AMLFDNR"] = " AND AMLFDNR LIKE '" + LFD.Trim() + "%'";
            dwData["AMPOS"] = " AND AMPOS LIKE '" + POS.Trim() + "%'";
            dwData["AMKST"] = " AND AMKST LIKE '" + AMKST.Trim() + "%'";
            dwData["AMTYP"] = " AND AMTYP LIKE '" + AMTYP.Trim() + "%'";
            dwData["AMART"] = " AND AMART LIKE '" + AMART.Trim() + "%'";
            SResult = TMV.QAM03(dwData);
            MergeData(SResult, true); 
            base.QueryCommand(sender, e);
        }

        public override void NewCommand(object sender, RoutedEventArgs e)
        {

            try
            {
                //这个地方会和查询条件冲突
                if (AMKST.Trim().Length < 2)
                {
                    throw new Exception("必须输入成本中心");
                }
                if (AMTYP.Trim().Length < 2)
                {
                    throw new Exception("必需输入类型");
                }

                if (AMART.Trim().Length < 2)
                {
                    throw new Exception("必需输入种类");

                }
                if (!IsNew)
                {
                    IsNew = true;
                    KRS = AMKST.Substring(0, 2);
                    LFD = AMKST.Substring(2, 4);
                    POS = "00";
                    return;
                }


                if (LFD.Trim().Length < 2)
                {
                    throw new Exception("必需输入编号");

                }
                if (KRS.Trim() == "M")
                {
                    IsMM.IsChecked = true;
                }

                if (IsMM.IsChecked == true)
                {
                    while (true)
                    {
                        dwData = TBMain.NewRow();
                        TMV.SetUpNewRow(ref dwData);
                        dwData["AMMANDANT"] = app.Mandant;
                        dwData["AMTYP"] = AMTYP;
                        dwData["AMART"] = AMART;
                        dwData["AMKSTKRS"] = AMKST.Substring(0, 2);
                        dwData["AMKST"] = AMKST.Substring(2, 4);
                        dwData["AMPKSTKRS"] = dwData["AMKSTKRS"];
                        dwData["AMPKST"] = dwData["AMKST"];
                        dwData["AMNRKRS"] = "M ";
                        LFD = Helpers.Helper.Right("0000" + (int.Parse(LFD) + 1).ToString(), 4);
                        dwData["AMLFDNR"] = LFD;
                        dwData["AMPOS"] = "00";
                        dwData["AMBEZ"] = (AMTYP.Trim() + int.Parse(LFD).ToString()).PadRight(40);
                        dwData["AMBDEMA"] = (dwData["AMNRKRS"].ToString().Trim() + int.Parse(dwData["AMLFDNR"].ToString())).PadRight(8);
                        Insert(dwData);
                        MLFD--;
                        if (MLFD <= 0)
                            break;
                    }
                }
                if (IsMP.IsChecked == true)
                {
                    while (true)
                    {
                        dwData = TBMain.NewRow();
                        TMV.SetUpNewRow(ref dwData);
                        dwData["AMMANDANT"] = app.Mandant;
                        dwData["AMTYP"] = AMTYP;
                        dwData["AMART"] = AMART;
                        dwData["AMKSTKRS"] = AMKST.Substring(0, 2);
                        dwData["AMKST"] = AMKST.Substring(2, 4);
                        dwData["AMPKSTKRS"] = dwData["AMKSTKRS"];
                        dwData["AMPKST"] = dwData["AMKST"];
                        dwData["AMNRKRS"] = KRS.PadRight(2);
                        POS = Helpers.Helper.Right("00" + (int.Parse(POS) + 1).ToString(), 2);
                        dwData["AMLFDNR"] = LFD;
                        dwData["AMPOS"] = POS;
                        dwData["AMBEZ"] = (AMTYP.Trim() + int.Parse(POS).ToString()).PadRight(40);
                        dwData["AMBDEMA"] = (dwData["AMNRKRS"].ToString().Trim() + int.Parse(dwData["AMPOS"].ToString())).PadRight(8);
                        Insert(dwData);
                        MPOS--;
                        if (MPOS <= 0)
                            break;
                    }
                }

                vwMaster.View.MoveCurrentToFirst();
                IsNew = false;

                base.NewCommand(sender, e);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        public override void DeleteCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
                return;
            TMV.DAM01(dwMaster);
            filter = "AMNRKRS='" + dwMaster["AMNRKRS"].ToString() + "' AND AMLFDNR='" + dwMaster["AMLFDNR"].ToString() + "' AND AMPOS='" + dwMaster["AMPOS"].ToString() + "'";
            rows = TBMain.Select(filter);
            vwMaster.View.MoveCurrentTo(-1);
            foreach (DataRow row in rows)
            {
                row.Delete();
            }
            TBMain.AcceptChanges();
            base.DeleteCommand(sender, e);
        }

        public override void SaveCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
                return;
            TMV.UpdateAMSTAMM(dwMaster, 0);
            base.SaveCommand(sender, e);
        }
        private void btnPrint_Click(object sender, RoutedEventArgs e)
        {

        }
        public override void OnLOChg(string lo)
        {
            if (lo.Length < 2)
                return;
            if (!IsEdit)
                return;
            TMV.AMLO = lo;
            base.OnLOChg(lo);
        }
        public override void OnLBChg(string lb)
        {
            if (lb.Length < 2)
                return;
            if (!IsEdit)
                return;
            TMV.AMLB = lb;
            base.OnLBChg(lb);
        }
        public override void OnSTDChg(bool value)
        {
            if (!IsEdit)
                return;
            if (IsSTD)
                TMV.AMSTDVAR = "J";
            else
                TMV.AMSTDVAR = "N";
            base.OnSTDChg(value);
        }
        public override void OnLPChg(string lp)
        {
            if (lp.Length < 8)
                return;
            if (!IsEdit)
                return;
            TMV.AMLPLATZ = lp;
            base.OnLPChg(lp);
        }
        public override void OnAMARTChg(string value)
        {
            if (value.Length < 8)
                return;
            if (!IsEdit)
                return;
            TMV.AMART = value;
            base.OnAMARTChg(value);
        }
        public override void OnAMTYPChg(string value)
        {
            if (value.Length < 8)
                return;
            if (!IsEdit)
                return;

            TMV.AMTYP = value;
            base.OnAMTYPChg(value);
        }
        public override void OnLIEFNRChg(string value)
        {
            if (!IsEdit)
                return;
            if (value.Length < 8)
                return;
            TMV.AMLIEFNR = value;
            base.OnLIEFNRChg(value);
        }
        public override void OnAMKSTChg(string value)
        {
            if (value.Length < 6)
                return;
            if (!IsEdit)
                return;

            TMV.AMKSTKRS = value.Substring(0, 2);
            TMV.AMKST = value.Substring(2, 4);
            TMV.AMPKSTKRS = TMV.AMKSTKRS;
            TMV.AMPKST = TMV.AMKST;
            base.OnAMKSTChg(value);
        }
        public override void OnAMCDChg(string value)
        {
            if (value.Length < 1)
                return;
            if (!IsEdit)
                return;
            TMV.AMVERFKZ = AMCD;
            base.OnAMCDChg(value);
        }
    }
}
